#include "renderer_win32.h"

void RendererWin32::init(HWND hwnd, int w, int h){ W=w; H=h; HDC hdc=GetDC(hwnd); mdc=CreateCompatibleDC(hdc); BITMAPINFO bi{}; bi.bmiHeader.biSize=sizeof(BITMAPINFOHEADER); bi.bmiHeader.biWidth=W; bi.bmiHeader.biHeight=-H; bi.bmiHeader.biPlanes=1; bi.bmiHeader.biBitCount=32; bi.bmiHeader.biCompression=BI_RGB; bmp=CreateDIBSection(hdc,&bi,DIB_RGB_COLORS,&pixels,nullptr,0); SelectObject(mdc,bmp); ReleaseDC(hwnd,hdc); }

void RendererWin32::resize(int w, int h){ if(w==W && h==H) return; if(bmp){ DeleteObject(bmp); bmp=nullptr; } W=w; H=h; BITMAPINFO bi{}; bi.bmiHeader.biSize=sizeof(BITMAPINFOHEADER); bi.bmiHeader.biWidth=W; bi.bmiHeader.biHeight=-H; bi.bmiHeader.biPlanes=1; bi.bmiHeader.biBitCount=32; bi.bmiHeader.biCompression=BI_RGB; bmp=CreateDIBSection(mdc,&bi,DIB_RGB_COLORS,&pixels,nullptr,0); SelectObject(mdc,bmp); }

void RendererWin32::begin(){ RECT r{0,0,W,H}; HBRUSH br=CreateSolidBrush(RGB(10,10,10)); FillRect(mdc,&r,br); DeleteObject(br); }

void RendererWin32::draw_body(const Body& b, const View& v, COLORREF c){ POINT p = to_screen(b.p,v); int r=6; HBRUSH br=CreateSolidBrush(c); HPEN pen=CreatePen(PS_SOLID,1,c); SelectObject(mdc,br); SelectObject(mdc,pen); Ellipse(mdc,p.x-r,p.y-r,p.x+r,p.y+r); DeleteObject(br); DeleteObject(pen); }

void RendererWin32::draw_trail(const Trail& t){ if(t.pts.size()<2) return; HPEN pen=CreatePen(PS_SOLID,1,t.color); SelectObject(mdc,pen); Polyline(mdc,t.pts.data(),(int)t.pts.size()); DeleteObject(pen); }

void RendererWin32::draw_text(int x, int y, const wchar_t* s){ SetBkMode(mdc,TRANSPARENT); SetTextColor(mdc,RGB(220,220,220)); TextOutW(mdc,x,y,s,(int)lstrlenW(s)); }

void RendererWin32::end(HWND hwnd){ HDC hdc=GetDC(hwnd); BitBlt(hdc,0,0,W,H,mdc,0,0,SRCCOPY); ReleaseDC(hwnd,hdc); }